#!/bin/bash
# vim:et sw=2 ts=2 ai syntax=sh:
set -e -u
[ $# -ne 1 ] && echo "Usage:$(basename $0) <pkgname>" && exit 1

. $(dirname $(realpath $0))/env
repoany="$repohost:$dirany"
repotest="$repohost:$dirtest"
listpath=$(pwd)
opt_conf=""
[ `uname -m` = sh4 ] && opt_conf="--config /etc/pacman.src.conf"
list_f=${listpath:-}/$1.makelist

go() {
  echo "  *** $@"
  "$@"
}
echoerr() { 
  echo "$@" 1>&2;
}
remove_old() {
  local line f
  while read line ; do
    if [ "$line" != "${line# *}" ] ;then
      echoerr "Err: strange line $line"
      exit 1
    fi
    f="$(cmp_version $line)"
    [ -n "$f" ] && echo "$f" >> $list_f
  done
}
pkgbuild_ver() { # <pkgname>
  ( cd ../$1 ; . ./PKGBUILD ; echo "$pkgver-$pkgrel" )
}
cmp_version() { # <pkgname>
  local orgname="$1" name vArch vInst vRepo vPkg
  name="${orgname#sh4-}"
  vInst=`pacman $opt_conf -Qi $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
  if [ -n "$vInst" ] ; then
    if [ "$orgname" = "$name" ] ; then
      vArch=`pacman $opt_conf -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
      if [ "$(vercmp $vArch $vInst)" = "1" ] ; then
        vRepo=`pacman -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
        if [ -z "$vRepo" ] ; then
          echo "$name # $vInst -> $vArch"
        elif [ "$(vercmp $vArch $vRepo)" = "1" ] ; then
          echo "$name # $vRepo -> $vArch"
        fi
      fi
    else # orgname is sh4- package
      vArch=`pacman $opt_conf -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
      vPkg="$(pkgbuild_ver $orgname)"
      if [ "$(vercmp $vArch $vPkg)" = "1" ] ; then
        echo "# Warn: $orgname # $vPkg -> $vArch manually"
      fi
    fi
  else # not installed
    [ -d ../sh4-$orgname ] && orgname="sh4-$name"
    vRepo=`pacman -Si $orgname 2>/dev/null |grep '^Version'|awk '{print $3}'`
    if [ "$orgname" = "$name" ] ; then
      vArch=`pacman $opt_conf -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
      if [ -z "$vRepo" ] ; then
        echo "$name # $vRepo -> $vArch"
      else
        if [ "$(vercmp $vArch $vRepo)" = 1 ] ; then
          echo "$name # $vRepo -> $vArch"
        else
          go yaourt -S --asdeps --needed --force --noconfirm $name
        fi
      fi
    else
      vArch=`pacman $opt_conf -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
      if [ -z "$vRepo" ] ; then
        echo "$orgname # $vRepo -> $vArch"
      else
        go yaourt -S --asdeps --needed --force --noconfirm $orgname
        vPkg="$(pkgbuild_ver $orgname)"
        if [ "$(vercmp $vArch $vPkg)" = "1" ] ; then
          echo "# Warn: $orgname # $vPkg -> $vArch manually"
        fi
      fi
    fi
  fi
}
build_pkg() { # <pkgname>
  local url file name="${1:-}" shortname vRepo vPkg v
  shortname=${name#sh4-}
  if [ "$name" != "$shortname" ] ; then
    vRepo=`pacman -Si $name 2>/dev/null |grep '^Version'|awk '{print $3}'`
    if [ -n "$vRepo" ] ; then
      vPkg="$(pkgbuild_ver $name)"
      case "$(vercmp $vPkg $vRepo)" in
      1) echo "## Build $name $vRepo -> $vPkg" ;;
      *) echo "## skip $name" ; return 0 ;;
      esac
    else
      echo "## Build $name # $vPkg"
    fi 
    cd ../$name
    go $listpath/get_local_src
    go makepkg -sL --noconfirm --needed     
    file=$(ls *.pkg.tar.*z)
  else
    echo "## Build $name"
    url=$(pacman -Sp $opt_conf $shortname 2>/dev/null | head -1)
    file=$(basename $url)
    case "$file" in
    *-any.pkg.*)
      echo "### get from any repo $name"
      go wget $url -O $file.part
      mv $file.part $file
      ;;
    *)
      if [ -d ../$name ] ; then
        vArch=`pacman $opt_conf -Si $shortname 2>/dev/null |grep '^Version'|awk '{print $3}'`
        vPkg="$(pkgbuild_ver $name)"
        if [ "$(vercmp $vArch $vPkg)" = "1" ] ; then
          cd .. ; go yaourt -G $opt_conf $name
          cd $listpath
        fi
      else
        cd .. ; go yaourt -G $opt_conf $name
        cd $listpath
      fi
      if [ ! -d ../$name ] ; then
        echo "Err: ../$name not exist, use .makelist instead"
      fi
      cd ../$name
      go $listpath/get_local_src
      go makepkg -AsL --noconfirm --needed     
      file=$(ls *.pkg.tar.*z)
      ;;
    esac
  fi
  go yaourt -U --asdeps --noconfirm $file
  $listpath/upd_arch_repo $file
  go yaourt -Syy --asdeps --noconfirm || true # prevent it return error code 1 as warnning
  cd $listpath
}
#rm -f $list_f
if [ ! -r $list_f ] ; then
  rc=0
  echo "Generating $list_f ..."
  pactree $opt_conf -slu $1 | grep -v -e '^lib32' -e multilib | tac > $list_f.1
  cat $list_f.1 |  remove_old # >  $list_f
  echo "# $list_f generated"
fi
echo "# building list $list_f"
cat $list_f | grep -v '^#' | while read line ; do
  echo "## makelist: $line"
  name=${line%% *}
  v="$(cmp_version $name)"
  if [ -z "$v" ] || [ -z "${v%%#*}" ] ; then
    continue
  fi
  if [ -r $listpath/$name.makelist ] ; then
    $0 $name
    continue
  fi
  build_pkg $name
done
echo "# done"

# makepkg -Asd --nocheck --skippgpcheck --skipinteg -L
